package org.aeon.gamechatnest.service.impl;

import org.aeon.gamechatnest.dto.AuthorityVo;
import org.aeon.gamechatnest.exception.TargetIsAnEmptyException;
import org.aeon.gamechatnest.mapper.*;
import org.aeon.gamechatnest.pojo.Role;
import org.aeon.gamechatnest.pojo.RoleAuthority;
import org.aeon.gamechatnest.pojo.RoleUser;
import org.aeon.gamechatnest.service.AuthorityService;
import org.aeon.gamechatnest.service.RoleService;
import org.aeon.gamechatnest.service.UserServerService;
import org.aeon.gamechatnest.util.ApplicationAssert;
import org.aeon.gamechatnest.util.AuthorityCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author: MissingAeon
 * @date: 2024/5/10 18:07
 * @description:
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleAuthorityMapper roleAuthorityMapper;

    @Autowired
    private RoleChannelMapper roleChannelMapper;

    @Autowired
    private RoleChannelAuthorityMapper roleChannelAuthorityMapper;

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Lazy
    @Autowired
    private AuthorityService authorityService;

    @Lazy
    @Autowired
    private UserServerService userServerService;

    @Override
    public boolean addRoleToServerUser(RoleUser roleUser) {
        return roleUserMapper.add(roleUser) > 0;
    }

    @Override
    public boolean addDefaultRoleToServer(Role role) {
        return roleMapper.add(role) > 0;
    }

    @Override
    public boolean addAuthorityToRole(RoleAuthority roleAuthority) {
        return roleAuthorityMapper.add(roleAuthority) > 0;
    }

    @Override
    public Set<String> findAuthorityByUserIdAndServerId(Long userId, long number) {
        //  查询用户在服务器中的角色
        List<Role> roles = roleMapper.getByUserIdAndServerId(userId, number);
        //  查询角色权限
        List<String> codes = authorityService.findAuthoritiesCodeByRoleIds(roles);
        return AuthorityCodeUtil.parse(codes);
    }

    @Override
    public boolean removeServerRole(Long serverId) {
        // 查询所有角色id
        List<Long> roleIds = roleMapper.getRoleIdsByServerId(serverId);
        // 查询所有在频道非默认权限角色
        List<Long> roleChannelIds = roleChannelMapper.getIdByRoleIds(roleIds);
        // 删除角色权限
        if (!roleIds.isEmpty()) {
            // 删除角色权限
            roleAuthorityMapper.deleteByRoleIds(roleIds);
            // 删除角色用户
            roleUserMapper.deleteByRoleIds(roleIds);
            // 删除角色
            roleMapper.deleteByIds(roleIds);
        }
        // 频道角色权限删除
        if (!roleChannelIds.isEmpty()) {
            // 删除在频道非默认权限角色权限
            roleChannelAuthorityMapper.batchDelete(roleIds);
            // 删除在频道非默认权限角色
            roleChannelMapper.batchDelete(roleChannelIds);
        }
        return true;
    }

    @Override
    public List<Role> findByServerId(Long serverId) {
        ApplicationAssert.isSuccess(userServerService.isExist(serverId), "服务器不存在");
        return roleMapper.getByServerId(serverId);
    }

    @Override
    public Role findById(Long roleId) {
        return null;
    }

    @Override
    public boolean isExist(Long roleId) {
        return roleMapper.isExist(roleId);
    }

    @Override
    public Long findServerIdByRoleId(Long roleId) throws TargetIsAnEmptyException {
        Long serverId = roleMapper.getServerIdById(roleId);
        ApplicationAssert.nonNull(serverId, "角色不存在");
        return serverId;
    }

    @Override
    public boolean save(Role role) {
        role.setCreateTime(new Date());
        return roleMapper.add(role) > 0;
    }

    @Override
    public boolean modify(Role role) {
        return roleMapper.update(role) > 0;
    }

    @Override
    public boolean remove(Long roleId) {
        return roleMapper.deleteByIds(List.of(roleId)) > 0;
    }

    @Override
    public boolean save(RoleAuthority roleAuthority) {
        return roleAuthorityMapper.add(roleAuthority) > 0;
    }

    @Override
    public boolean remove(RoleAuthority roleAuthority) {
        return roleAuthorityMapper.delete(roleAuthority) > 0;
    }
}
